﻿namespace TPXSDK.NET.UObject;

/// <summary>
/// 元数据类型编码
/// </summary>
public enum MetaDataType
{
    DT_U8 = 0x00,  //   8-bit unsigned integer
    DT_S8 = 0x01,  //   8-bit   signed integer
    DT_U16 = 0x02,  //  16-bit unsigned integer
    DT_S16 = 0x03,  //  16-bit   signed integer
    DT_U32 = 0x04,  //  32-bit unsigned integer
    DT_S32 = 0x05,  //  32-bit   signed integer
    DT_FP32 = 0x06   //  32-bit floating point
}

/// <summary>
/// UObject Meta结构，用于描述对象结构
/// </summary>
public class UObjMeta
{
    /// <summary>
    /// 类型名称
    /// </summary>
    public string TypeName { get; set; } = string.Empty;
    /// <summary>
    /// 类型ID
    /// </summary>
    public byte TypeId { get; set; }
    /// <summary>
    /// 对象数据大小
    /// </summary>
    public byte TypeSz { get; set; }
    /// <summary>
    /// 实例个数
    /// </summary>
    public ushort InstCnt { get; set; }
    /// <summary>
    /// 是否为配置项
    /// </summary>
    public bool IsSetting { get; set; }

    /// <summary>
    /// 成员变量个数
    /// </summary>
    public int MemCnt
    {
        get => MemName?.Length ?? 0;
        set
        {
            if (value == MemName?.Length) return;
            MemName = new string[value];
            MemUnit = new string[value];
            MemType = new MetaDataType[value];
        }
    }

    /// <summary>
    /// 成员变量大小
    /// </summary>
    public int MemSize(int idx)
    {
        if (idx < MemCnt)
        {
            if (MemType[idx] == MetaDataType.DT_U8 ||
                MemType[idx] == MetaDataType.DT_S8)
            {
                return 1;
            }

            if (MemType[idx] == MetaDataType.DT_U16 ||
                MemType[idx] == MetaDataType.DT_S16)
            {
                return 2;
            }

            if (MemType[idx] == MetaDataType.DT_U32 ||
                MemType[idx] == MetaDataType.DT_S32 ||
                MemType[idx] == MetaDataType.DT_FP32)
            {
                return 4;
            }
        }
        return 0;
    }

    /// <summary>
    /// 成员变量名称
    /// </summary>
    public string[] MemName { get; set; } = [];

    /// <summary>
    /// 成员变量单位
    /// </summary>
    public string[] MemUnit { get; set; } = [];

    /// <summary>
    /// 成员变量类型
    /// </summary>
    public MetaDataType[] MemType { get; set; } = [];
}
